package com.ruoyi.boss.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Service;
import com.ruoyi.boss.mapper.TFenchMapper;
import com.ruoyi.boss.domain.TFench;
import com.ruoyi.boss.service.ITFenchService;

/**
 * 下单围栏列表优化Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-18
 */
@Service
public class TFenchServiceImpl implements ITFenchService 
{
    @Autowired
    private TFenchMapper tFenchMapper;

    @Autowired
    KafkaTemplate kafkaTemplate;

    /**
     * 查询下单围栏列表优化
     * 
     * @param fenchId 下单围栏列表优化主键
     * @return 下单围栏列表优化
     */
    @Override
    public TFench selectTFenchByFenchId(Long fenchId)
    {
        return tFenchMapper.selectTFenchByFenchId(fenchId);
    }

    /**
     * 查询下单围栏列表优化列表
     * 
     * @param tFench 下单围栏列表优化
     * @return 下单围栏列表优化
     */
    @Override
    public List<TFench> selectTFenchList(TFench tFench)
    {
        return tFenchMapper.selectTFenchList(tFench);
    }

    /**
     * 新增下单围栏列表优化
     * 
     * @param tFench 下单围栏列表优化
     * @return 结果
     */
    @Override
    public int insertTFench(TFench tFench)
    {
        tFench.setCreateTime(DateUtils.getNowDate());

        String msg = JSON.toJSONString(tFench);
        kafkaTemplate.send("topic_fench", msg);
        System.out.println("发送kafka消息"+msg);

        return tFenchMapper.insertTFench(tFench);
    }

    /**
     * 接收kafka消息
     * @param msg
     * @param acknowledgment
     */
    @KafkaListener(topics = "topic_fench")
    public void reciveMsg(String msg, Acknowledgment acknowledgment){

        //转换成对象
        TFench tFench = JSON.parseObject(msg, TFench.class);
        String fenchName = tFench.getFenchName();
        System.out.println("添加的围栏名称是："+fenchName);
        //手动提交
        acknowledgment.acknowledge();
    }

    /**
     * 修改下单围栏列表优化
     * 
     * @param tFench 下单围栏列表优化
     * @return 结果
     */
    @Override
    public int updateTFench(TFench tFench)
    {
        tFench.setUpdateTime(DateUtils.getNowDate());
        return tFenchMapper.updateTFench(tFench);
    }

    /**
     * 批量删除下单围栏列表优化
     * 
     * @param fenchIds 需要删除的下单围栏列表优化主键
     * @return 结果
     */
    @Override
    public int deleteTFenchByFenchIds(Long[] fenchIds)
    {
        return tFenchMapper.deleteTFenchByFenchIds(fenchIds);
    }

    /**
     * 删除下单围栏列表优化信息
     * 
     * @param fenchId 下单围栏列表优化主键
     * @return 结果
     */
    @Override
    public int deleteTFenchByFenchId(Long fenchId)
    {
        return tFenchMapper.deleteTFenchByFenchId(fenchId);
    }
}
